{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "21b4a9d2",
   "metadata": {},
   "source": [
    "# 1. 音频录制"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d2e33ed",
   "metadata": {},
   "source": [
    "- 安装下载：pip install 模块名称\n",
    "> 1. SpeechRecognition\n",
    "> 2. Pydub\n",
    "> 3. pyAudio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "24b38187",
   "metadata": {},
   "outputs": [],
   "source": [
    "#录制\n",
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"2.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "beee6aef",
   "metadata": {},
   "source": [
    "# 2. 语音识别\n",
    ">1. 百度API：百度的AI账户 --（Key，secret）\n",
    ">2. I/O：input 音频文件（pcm/mav/amr）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1861ee05",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bdasr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4121cfb5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "token http response http code : 401\n",
      "{\"error_description\":\"unknown client id\",\"error\":\"invalid_client\"}\n",
      "\n",
      "{'error_description': 'unknown client id', 'error': 'invalid_client'}\n"
     ]
    },
    {
     "ename": "DemoError",
     "evalue": "MAYBE API_KEY or SECRET_KEY not correct: access_token or scope not found in token response",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mDemoError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[3], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m bdasr_result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28meval\u001b[39m(\u001b[43mbdasr\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43masr\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m2.wav\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m)[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mresult\u001b[39m\u001b[38;5;124m'\u001b[39m][\u001b[38;5;241m0\u001b[39m]\n\u001b[0;32m      2\u001b[0m bdasr_result\n",
      "File \u001b[1;32mD:\\API\\api\\week13\\bdasr.py:101\u001b[0m, in \u001b[0;36masr\u001b[1;34m(AUDIO_FILE)\u001b[0m\n\u001b[0;32m     99\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21masr\u001b[39m(AUDIO_FILE):\n\u001b[0;32m    100\u001b[0m     FORMAT \u001b[38;5;241m=\u001b[39m AUDIO_FILE[\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m3\u001b[39m:]  \u001b[38;5;66;03m# 文件后缀只支持 pcm/wav/amr 格式，极速版额外支持m4a 格式\u001b[39;00m\n\u001b[1;32m--> 101\u001b[0m     token \u001b[38;5;241m=\u001b[39m \u001b[43mfetch_token\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    103\u001b[0m     speech_data \u001b[38;5;241m=\u001b[39m []\n\u001b[0;32m    104\u001b[0m     \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mopen\u001b[39m(AUDIO_FILE, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mrb\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;28;01mas\u001b[39;00m speech_file:\n",
      "File \u001b[1;32mD:\\API\\api\\week13\\bdasr.py:95\u001b[0m, in \u001b[0;36mfetch_token\u001b[1;34m()\u001b[0m\n\u001b[0;32m     93\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m result[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124maccess_token\u001b[39m\u001b[38;5;124m'\u001b[39m]\n\u001b[0;32m     94\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m---> 95\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m DemoError(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mMAYBE API_KEY or SECRET_KEY not correct: access_token or scope not found in token response\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
      "\u001b[1;31mDemoError\u001b[0m: MAYBE API_KEY or SECRET_KEY not correct: access_token or scope not found in token response"
     ]
    }
   ],
   "source": [
    "bdasr_result = eval(bdasr.asr(\"2.wav\"))['result'][0]\n",
    "bdasr_result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b925825d",
   "metadata": {},
   "source": [
    "# 3. 连接自动回复字典（openai-chatgpt）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b03acfbe",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa = {\n",
    "    '好棒呀。':\"你也好棒呀\",\n",
    "    '北京科技馆。':\"中国科技馆是我国唯一的国家级综合性科技馆，是实施科教兴国战略、人才强国战略和创新驱动发展战略， 提高全民科学素质的大型科普基础设施。\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4067e839",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa_result = qa.get(bdasr_result)\n",
    "qa_result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d1df53b",
   "metadata": {},
   "source": [
    "## 3.1 接入chatgpt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85e5e2e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "! pip install openai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3e59faf5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import opchat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39e192ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "opchat.openai_chat('hello')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2699e4d2",
   "metadata": {},
   "source": [
    "# 4. 语音合成"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e802d6f",
   "metadata": {},
   "source": [
    "> 1. 百度API：百度的AI账户（key，secret)\n",
    "> 2. I/O:input文本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44dc089f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import bdtts\n",
    "bdtts.tts(qa_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b3d03e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install SpeechRecognition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "31983081",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install pyaudio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7efcfd1e",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install pydub"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47202754",
   "metadata": {},
   "source": [
    "# 5. 播放音频"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "36ae2d6e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pydub import AudioSegment\n",
    "from pydub.playback import play\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f50d0b7a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e31d0a8b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
